Gaussian Process Classification
Preliminary steps
Loading necessary packages
using Plots
using HTTP, CSV
using DataFrames: DataFrame
using AugmentedGaussianProcessesLoading the banana dataset from OpenML
data = HTTP.get("https://www.openml.org/data/get_csv/1586217/phpwRjVjk")
data = CSV.read(data.body, DataFrame)
data.Class[data.Class .== 2] .= -1
data = Matrix(data)
X = data[:, 1:2]
Y = Int.(data[:, end]);We create a function to visualize the data
function plot_data(X, Y; size=(300, 500))
return Plots.scatter(
eachcol(X)...; group=Y, alpha=0.2, markerstrokewidth=0.0, lab="", size=size
)
end
plot_data(X, Y; size=(500, 500))
Run sparse classification with increasing number of inducing points
Ms = [4, 8, 16, 32, 64]
models = Vector{AbstractGPModel}(undef, length(Ms) + 1)
kernel = SqExponentialKernel() ∘ ScaleTransform(1.0)
for (i, num_inducing) in enumerate(Ms)
@info "Training with $(num_inducing) points"
global m = SVGP(
kernel,
LogisticLikelihood(),
AnalyticVI(),
inducingpoints(KmeansAlg(num_inducing), X);
optimiser=false,
Zoptimiser=false,
)
@time train!(m, X, Y, 20)
models[i] = m
end[ Info: Training with 4 points
0.008732 seconds (3.00 k allocations: 16.952 MiB)
[ Info: Training with 8 points
0.012805 seconds (3.02 k allocations: 27.585 MiB)
[ Info: Training with 16 points
0.022346 seconds (3.04 k allocations: 48.981 MiB)
[ Info: Training with 32 points
0.041561 seconds (3.09 k allocations: 92.321 MiB)
[ Info: Training with 64 points
0.199127 seconds (3.37 k allocations: 181.211 MiB, 47.32% gc time)Running the full model
@info "Running full model"
mfull = VGP(X, Y, kernel, LogisticLikelihood(), AnalyticVI(); optimiser=false)
@time train!(mfull, 5)
models[end] = mfullVariational Gaussian Process with a Bernoulli Likelihood with Logistic Link infered by Analytic Variational Inference We create a prediction and plot function on a grid
function compute_grid(model, n_grid=50)
mins = [-3.25, -2.85]
maxs = [3.65, 3.4]
x_lin = range(mins[1], maxs[1]; length=n_grid)
y_lin = range(mins[2], maxs[2]; length=n_grid)
x_grid = Iterators.product(x_lin, y_lin)
y_grid, _ = proba_y(model, vec(collect.(x_grid)))
return y_grid, x_lin, y_lin
end
function plot_model(model, X, Y, title=nothing; size=(300, 500))
n_grid = 50
y_pred, x_lin, y_lin = compute_grid(model, n_grid)
title = if isnothing(title)
(model isa SVGP ? "M = $(AGP.dim(model[1]))" : "full")
else
title
end
p = plot_data(X, Y; size=size)
Plots.contour!(
p,
x_lin,
y_lin,
reshape(y_pred, n_grid, n_grid)';
cbar=false,
levels=[0.5],
fill=false,
color=:black,
linewidth=2.0,
title=title,
)
if model isa SVGP
Plots.scatter!(
p, eachrow(hcat(AGP.Zview(model[1])...))...; msize=2.0, color="black", lab=""
)
end
return p
end;Now run the prediction for every model and visualize the differences
Plots.plot(
plot_model.(models, Ref(X), Ref(Y))...; layout=(1, length(models)), size=(1000, 200)
)
Bayesian SVM vs Logistic
We now create a model with the Bayesian SVM likelihood
mbsvm = VGP(X, Y, kernel, BayesianSVM(), AnalyticVI(); optimiser=false)
@time train!(mbsvm, 5)(Variational Gaussian Process with a Bayesian SVM infered by Analytic Variational Inference , (local_vars = (ω = [0.2670231563486319, 0.36724850110706503, 0.008274352901012624, 0.12202954702193707, 0.24779122630014175, 5.302032576191675, 1.598446592399752, 1.9596043324888275, 0.6847119281828133, 0.13062766416437857 … 0.04043278780186586, 0.006683983485525364, 1.8586979168314721, 1.665273038282036, 0.013928697741228837, 4.882294630899407, 1.8932642381324083, 2.2637571965053884, 0.552540403072177, 5.1256273171661], θ = [1.9351985832577017, 1.6501369305257532, 10.993424045876132, 2.862645042408495, 2.0088940733695604, 0.4342889747617259, 0.7909534686352021, 0.7143578220529614, 1.2084983260164743, 2.766829640147997 … 4.973168300227987, 12.231573121326033, 0.7334923587790049, 0.7749207224515071, 8.473147037655298, 0.4525723404146194, 0.7267656360605215, 0.6646378623081302, 1.3452963996542784, 0.4416990724035967]), opt_state = (NamedTuple(),), hyperopt_state = (NamedTuple(),), kernel_matrices = ((K = LinearAlgebra.Cholesky{Float64, Matrix{Float64}}([1.0000499987500624 0.017000746952647305 … 0.6304203262759929 0.377775755997342; 0.017001596968745064 0.9999054828347788 … 0.0007664332243453775 0.26392364754823905; … ; 0.6304518465043205 0.011483977224073186 … 0.0100268860964567 3.4159402770416343e-6; 0.3777946443129457 0.27032117226579383 … 0.38985281832414914 0.010036620197432776], 'U', 0),),)))And compare it with the Logistic likelihood
Plots.plot(
plot_model.(
[models[end], mbsvm], Ref(X), Ref(Y), ["Logistic", "BSVM"]; size=(500, 500)
)...;
layout=(1, 2),
)
This page was generated using Literate.jl.